package com.ikangtai.shecare.stickycalendar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.stickycalendar.bean.MoreReBean;
import com.ikangtai.shecare.stickycalendar.http.OvulationDayHttp;
import com.ikangtai.shecare.stickycalendar.http.util.OvulationDayMsg;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by lijinyuan on 16/8/10.
 */
public class RecordMoreAdapter extends BaseAdapter implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private List<MoreReBean> mlist;
    private Context mContext;
    private ViewHolder viewHolder;

    public RecordMoreAdapter(Context context, List<MoreReBean> list) {
        this.mContext = context;
        this.mlist = list;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.more_re_item, null);
            viewHolder.title_switch = (ToggleButton) convertView.findViewById(R.id.title_switch);
            viewHolder.title_img = (ImageView) convertView.findViewById(R.id.title_img);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.title_icon = (ImageView) convertView.findViewById(R.id.title_icon);
            viewHolder.title_img_more = (ImageView) convertView.findViewById(R.id.title_img_more);


            switch (mlist.get(position).getRightType()) {

                case 0:
                    viewHolder.title_img_more.setVisibility(View.GONE);
                    viewHolder.title_switch.setVisibility(View.GONE);
                    viewHolder.title_img.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    viewHolder.title_switch.setVisibility(View.VISIBLE);
                    viewHolder.title_img.setVisibility(View.GONE);
                    viewHolder.title_img_more.setVisibility(View.GONE);


                    viewHolder.title_switch.setOnCheckedChangeListener(this);
                    break;
                case 3:
                    viewHolder.title_switch.setVisibility(View.GONE);
                    viewHolder.title_img.setVisibility(View.GONE);
                    viewHolder.title_img_more.setVisibility(View.VISIBLE);
                    break;
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.title_icon.setImageResource(mlist.get(position).getImg());
        viewHolder.title.setText(mlist.get(position).getTitle());

        return convertView;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            List<OvulationDayMsg> ovulationDayMsgList = new ArrayList<OvulationDayMsg>();
            OvulationDayMsg ovulationDayMsg = new OvulationDayMsg();
            ovulationDayMsg.setDateRecord("2016-08-29");
            ovulationDayMsg.setIsOvulationDay(1);
            ovulationDayMsgList.add(ovulationDayMsg);
            OvulationDayHttp ovulationDayHttp = new OvulationDayHttp(mContext, ovulationDayMsgList);
            ovulationDayHttp.saveSelectedDayRecord();
            AppLog.i("wzz------开");
        } else {
            List<OvulationDayMsg> ovulationDayMsgList = new ArrayList<OvulationDayMsg>();
            OvulationDayMsg ovulationDayMsg = new OvulationDayMsg();
            ovulationDayMsg.setDateRecord("2016-08-29");
            ovulationDayMsg.setIsOvulationDay(0);
            ovulationDayMsgList.add(ovulationDayMsg);
            OvulationDayHttp ovulationDayHttp = new OvulationDayHttp(mContext, ovulationDayMsgList);
            ovulationDayHttp.saveSelectedDayRecord();
            AppLog.i("wzz------关");
        }

    }

    @Override
    public void onClick(View v) {

    }

    static class ViewHolder {
        private TextView title;
        private ImageView title_icon;
        private ImageView title_img, title_img_more;
        private ToggleButton title_switch;

    }
}
